<template>
  <div class="bigbox">
    <!-- 页面导航搜索盒子 -->
    <div class="topbox">
      <el-form ref="form" label-position="left" label-width="80px">
        <el-row :gutter="20">
          <el-col :span="5">
            <el-form-item label="建筑群">
              <FilterSelect2
                v-model="buildingId"
                :optionlist="qiyelist"
                @updateModelValue="changeqiye"
                :valuekey="{ name: 'name', value: 'id' }"
              ></FilterSelect2>
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="用户名称">
              <el-input
                size="small"
                v-model="userName"
                placeholder="请输入用户名称"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="是否绑定">
              <el-select
                v-model="bindFlag"
                placeholder="请选择"
                size="small"
                style="width: 100%"
              >
                <el-option label="是" value="1"></el-option>
                <el-option label="否" value="0"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="9" style="text-align: right">
            <el-form-item label="">
              <el-button size="small" @click="chongzhi">重 置</el-button>
              <el-button type="primary" size="small" @click="gettablelist()"
                >查 询</el-button
              >
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <!-- 盒子主体部分 -->
    <div class="bodybox">
      <div class="title">
        <div>用户信息</div>
        <div>
          <TopTile
            @command="command"
            @daochu="daochu"
            @shuaxin="shuaxin"
          ></TopTile>
        </div>
      </div>
      <div class="table">
        <TableColumn
          ref="tabledata"
          :tableData="tableData"
          :tableConfig="tableConfig"
          :issize="issize"
        >
          <el-table-column label="操作" align="center">
            <template #default="{ row }">
              <el-link
                type="primary"
                style="margin-right: 10px"
                :underline="false"
                @click="$refs.PutPasswordrefs.toput(row)"
                >重置密码</el-link
              >
              <el-link type="primary" :underline="false" @click="del(row)"
                >解绑</el-link
              >
            </template>
          </el-table-column>
        </TableColumn>
      </div>
      <!-- 底部分页 -->
      <div class="fenye">
        <el-pagination
          background
          layout="prev, pager, next"
          :current-page.sync="pageobj.pageNo"
          :page-size="20"
          @current-change="gettablelist"
          :total="+totalCount"
        >
        </el-pagination>
      </div>
      <PutPassword ref="PutPasswordrefs" @getlist="gettablelist"></PutPassword>
    </div>
  </div>
</template>

<script>
import PutPassword from './put-password.vue'
import { getUserListapi,postWebUnbindapi } from '@/api/prwpaid/configura/UserCode'
import { getloginlistapi } from '@/api/prwpaid/file-management/Architectural-complex'
import TopTile from '@/components/TopTitle/index.vue'
import TableColumn from '@/components/Table/index.vue'
import { exportexcel } from '@/utils/excel'
export default {
  name: 'CallPoliceIndex',
  components: {
    TopTile,
    TableColumn,
    PutPassword
  },
  data() {
    return {
      resetFlag: '',
      issize: 'medium',
      buildingId: '',
      userName: '',
      bindFlag: '',
      qiyelist: [],
      tableData: [],
      tableConfig: [
        { label: '序号', type: 'index', center: 'center', width: '100' },
        { label: '建筑群', prop: 'buildingName', width: '240' },
        { label: '用户名称', prop: 'userName' },
        { label: '手机号', prop: 'phone' },
        { label: '微信ID', prop: 'wxOpenid' },
        { label: '是否绑定', prop: 'bindFlagName' }
      ],
      pageobj: {
        pageNo: 1,
        pageSize: 20
      },
      totalCount: ''
    }
  },

  async mounted() {
    const res = await getloginlistapi()
    this.qiyelist = res.data.list
    const localbuildingId = localStorage.getItem('buildingId')
    const isBuildIdInArray = this.qiyelist.some(
      (item) => item.id == localbuildingId
    )
    if (isBuildIdInArray) {
      this.buildingId = localbuildingId ? localbuildingId : ''
    } else {
      this.buildingId = ''
    }
    this.gettablelist()
  },

  methods: {
    // 企业下拉
    changeqiye(val) {
      localStorage.setItem('buildingId', val)
    },
    // 获取表格数据
    async gettablelist() {
      this.$refs.tabledata.loading = true
      const {
        data: { list, totalCount }
      } = await getUserListapi({
        buildingId: this.buildingId,
        userName: this.userName,
        pageNo: this.pageobj.pageNo - 1,
        pageSize: this.pageobj.pageSize,
        bindFlag: this.bindFlag
      })
      this.tableData = list
      this.totalCount = totalCount
      this.$refs.tabledata.loading = false
    },
    // 重置
    chongzhi() {
      localStorage.removeItem('buildingId')
      this.bindFlag = ''
      this.buildingId = ''
      this.userName = ''
      this.gettablelist()
    },
    // 调节表格密度
    command(val) {
      if (val === '默认') {
        this.issize = 'medium'
      } else if (val === '中等') {
        this.issize = 'small'
      } else {
        this.issize = 'mini'
      }
    },
    del(val) {
      this.$confirm('此操作将解绑该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const res= await postWebUnbindapi(val)
          this.$message({
            type:res.code=='200'?'success':'warning',
            message:res.code=='200'?'解绑成功':res.message,
          })
          if(res.code=='200') this.gettablelist()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 导出
    daochu() {
      exportexcel(
        'mbm-microservice-biz/mbm/customer/web/user/list/exportExcel',
        {
          buildingId: this.buildingId,
          userName: this.userName,
          pageNo: this.pageobj.pageNo - 1,
          pageSize: this.pageobj.pageSize,
          bindFlag: this.bindFlag
        },
        '用户信息'
      )
    },
    // 刷新列表
    shuaxin() {
      this.gettablelist()
      this.$message.success('列表刷新成功')
    }
  }
}
</script>

<style lang="scss" scoped>
.option {
  padding-left: 10px;
}
.bigbox {
  height: 100%;
  padding: 10px;
  .topbox {
    height: 80px;
    padding: 20px;
    background-color: #fff;
    margin-bottom: 10px;
  }
  .bodybox {
    height: calc(100% - 90px);
    padding: 20px;
    background-color: #fff;
    .title {
      margin-bottom: 10px;
      display: flex;
      justify-content: space-between;
    }
    .table {
      height: 90%;
    }
    .fenye {
      text-align: right;
    }
  }
}
</style>
